using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;
using System.Data;
using System.ComponentModel;
using Jonathan.SharpDB;
using System.IO;

namespace Jonathan
{
	#region -= Bible Row comparer =-

	/// <summary>
	/// Compares two bible passages from search.
	/// </summary>
    public class BibleRowComparer : IComparer<string[]>
    {
        private ListSortDirection m_Direction;
        private Query m_Query;

		/// <summary>
		/// Creates a new Bible Row Comparer.
		/// </summary>
		/// <param name="direction">Sort Directions</param>
		/// <param name="dbPath">Path to Database.</param>
        public BibleRowComparer (ListSortDirection direction, string dbPath)
        {
            this.m_Direction = direction;

			if (!string.IsNullOrEmpty(dbPath))
			{
				// Get a list of books in database.
				Database db = new Database(dbPath);
				m_Query = new Query(QueryType.SELECT, Common.TABLE_BOOK_LIST);
				m_Query.SELECT("*");
				db.RunQuery(m_Query);
			}
		}

		#region -= Compare =-

		/// <summary>
		/// Sorts the given passages.
		/// </summary>
		/// <param name="row1">Firt Passage</param>
		/// <param name="row2">Second Passage</param>
		/// <returns>-1,0,1 if the first passage is less than, equal, or greater than the second passage.</returns>
        public int Compare (string[] row1, string[] row2)
        {
            string book1 = row1[0];
            string book2 = row2[0];

            int b1 = 0;
            int b2 = 0;
            int cnt = 0;
            for (int a = 0; a < this.m_Query.RowsReturned; a++)
            {
                if(this.m_Query.Results.GetFieldString(0, a).Equals(book1))
                {
                    b1 = this.m_Query.Results.GetFieldInt(1, a);
                    cnt++;
                }
                if (this.m_Query.Results.GetFieldString(0, a).Equals(book2))
                {
                    b2 = this.m_Query.Results.GetFieldInt(1, a);
                    cnt++;
                }
                if (cnt == 2)
                    break;
            }

            if (b1 != b2)
            {
                return (this.m_Direction == ListSortDirection.Ascending) ?
                            b1.CompareTo(b2) : b2.CompareTo(b1);
            }
            else
            {
                string cvInfo1 = Common.RemoveHtml(row1[1].Trim());
                string cvInfo2 = Common.RemoveHtml(row2[1].Trim());
                int c1 = Convert.ToInt32(cvInfo1.Split(':')[0]);
                int c2 = Convert.ToInt32(cvInfo2.Split(':')[0]);
                if (c1 != c2)
                {
                    return (this.m_Direction == ListSortDirection.Ascending) ?
                                c1.CompareTo(c2) : c2.CompareTo(c1);
                }
                else
                {
                    int v1 = Convert.ToInt32(cvInfo1.Split(':')[1]);
                    int v2 = Convert.ToInt32(cvInfo2.Split(':')[1]);
                    return (this.m_Direction == ListSortDirection.Ascending) ?
                                v1.CompareTo(v2) : b2.CompareTo(v1);
                }
            }
		}

		#endregion
	}

	#endregion

	#region -= Reference Row Comparer =-

	/// <summary>
	/// Compares two reference words  from search.
	/// </summary>
	public class RefRowComparer : IComparer<string[]>
    {
        private ListSortDirection m_Direction;

		/// <summary>
		/// Create a new Reference Row Comparer
		/// </summary>
		/// <param name="direction">Sort direction.</param>
        public RefRowComparer (ListSortDirection direction)
        {
            this.m_Direction = direction;
        }

		/// <summary>
		/// Compares the given reference words.
		/// </summary>
		/// <param name="row1">First Reference</param>
		/// <param name="row2">Second Reference</param>
		/// <returns>-1,0,1 if the first reference is less than, equal, or greater than the second reference.</returns>
        public int Compare (string[] row1, string[] row2)
        {
            string book1 = row1[0];
            string book2 = row2[0];

            if (book1 != book2)
            {
                return (this.m_Direction == ListSortDirection.Ascending) ?
                            book1.CompareTo(book2) : book2.CompareTo(book1);
            }
            else
            {
                string word1 = row1[1];
                string word2 = row2[1];
                return (this.m_Direction == ListSortDirection.Ascending) ?
                            word1.CompareTo(word2) : word2.CompareTo(word1);
            }
        }
	}

	#endregion

	#region -= Verse List comparer =-

	/// <summary>
	/// Compares Two verses.
	/// </summary>
	public class NewVerseListComparer : IComparer<string[]>
    {
		/// <summary>
		/// Creates a new Verse List comparer.
		/// </summary>
        public NewVerseListComparer ()
        {
        }

		/// <summary>
		/// Compares the given verses.
		/// </summary>
		/// <param name="row1">First Verse.</param>
		/// <param name="row2">Second Verse.</param>
		/// <returns>-1,0,1 if the first verse is less than, equal, or greater than the second verse.</returns>
        public int Compare (string[] row1, string[] row2)
        {
            return row1[2].CompareTo(row2[2]);
        }
	}

	#endregion

	#region -= Passage Row Comparer =-

	/// <summary>
	/// Compares two passages from search.
	/// </summary>
	public class PassageRowComparer : IComparer<string[]>
    {
        private ListSortDirection m_Direction;
        private Query m_Query;

		/// <summary>
		/// Creates a new Passage Row comparer.
		/// </summary>
		/// <param name="direction">Sort Direction.</param>
		/// <param name="dbPath">Path to database.</param>
        public PassageRowComparer (ListSortDirection direction, string dbPath)
        {
            this.m_Direction = direction;

            if (!string.IsNullOrEmpty(dbPath))
            {
                Database db = new Database(dbPath);
                m_Query = new Query(QueryType.SELECT, Common.TABLE_BOOK_LIST);
                m_Query.SELECT("*");
                db.RunQuery(m_Query);
            }
		}

		#region -= Compare =-

		/// <summary>
		/// Sorts the given passages.
		/// </summary>
		/// <param name="row1">First Passage</param>
		/// <param name="row2">Second Passage</param>
		/// <returns>-1,0,1 if the first passage is less than, equal, or greater than the second passage.</returns>
        public int Compare (string[] row1, string[] row2)
        {
            string source1 = row1[0];
            string source2 = row2[0];

            string info1 = row1[1].Trim();
            string info2 = row2[1].Trim();
            string book1 = "";
            string book2 = "";
            string cvInfo1 = "";
            string cvInfo2 = "";
            if (info1.Contains(" "))
            {
                book1 = info1.Substring(0, info1.LastIndexOf(' '));//.Split(' ')[0];
                book2 = info2.Substring(0, info2.LastIndexOf(' '));//.Split(' ')[0];
                cvInfo1 = info1.Substring(info1.LastIndexOf(' ') + 1);
                cvInfo2 = info2.Substring(info2.LastIndexOf(' ') + 1);
            }
            else
            {
                cvInfo1 = info1;
                cvInfo2 = info2;
            }

            int b1 = 0;
            int b2 = 0;

            if (this.m_Query != null)
            {
                int cnt = 0;
                for (int a = 0; a < this.m_Query.RowsReturned; a++)
                {
                    if (this.m_Query.Results.GetFieldString(0, a).Equals(book1))
                    {
                        b1 = this.m_Query.Results.GetFieldInt(1, a);
                        cnt++;
                    }
                    if (this.m_Query.Results.GetFieldString(0, a).Equals(book2))
                    {
                        b2 = this.m_Query.Results.GetFieldInt(1, a);
                        cnt++;
                    }
                    if (cnt == 2)
                        break;
                }
            }

            if (b1 != b2)
            {
                return (this.m_Direction == ListSortDirection.Ascending) ?
                            b1.CompareTo(b2) : b2.CompareTo(b1);
            }
            else
            {
                //string cvInfo1 = info1.Substring(info1.LastIndexOf(' ') + 1);//.Split(' ')[1];
                //string cvInfo2 = info2.Substring(info2.LastIndexOf(' ') + 1);//.Split(' ')[1];
                int c1 = Convert.ToInt32(cvInfo1.Split(':')[0]);
                int c2 = Convert.ToInt32(cvInfo2.Split(':')[0]);
                if (c1 != c2)
                {
                    return (this.m_Direction == ListSortDirection.Ascending) ?
                                c1.CompareTo(c2) : c2.CompareTo(c1);
                }
                else
                {
                    int v1 = Convert.ToInt32(cvInfo1.Split(':')[1]);
                    int v2 = Convert.ToInt32(cvInfo2.Split(':')[1]);
                    return (this.m_Direction == ListSortDirection.Ascending) ?
                                v1.CompareTo(v2) : b2.CompareTo(v1);
                }
            }
		}

		#endregion
	}

	#endregion

	#region -= Passage Grid Comparer =-

	/// <summary>
	/// Sorts passages from the book mark control.
	/// </summary>
	public class PassageGridComparer : IComparer
    {
        private ListSortDirection m_Direction;
        private Query m_Query;

		/// <summary>
		/// Creates a new Passage Grid Comparer
		/// </summary>
		/// <param name="direction">Sort direction</param>
		/// <param name="dbPath">Path to Database</param>
        public PassageGridComparer (ListSortDirection direction, string dbPath)
        {
            this.m_Direction = direction;

            if (!string.IsNullOrEmpty(dbPath))
            {
                Database db = new Database(dbPath);
                m_Query = new Query(QueryType.SELECT, Common.TABLE_BOOK_LIST);
                m_Query.SELECT("*");
                db.RunQuery(m_Query);
            }
		}

		#region -= Compare =-

		/// <summary>
		/// Sorts the given object.
		/// </summary>
		/// <param name="x">OutlookGrid.OutlookGridRow row1</param>
		/// <param name="y">OutlookGrid.OutlookGridRow row2</param>
		/// <returns>-1,0,1 if the first passage is less than, equal, or greater than the second passage.</returns>
        public int Compare (object x, object y)
        {
            OutlookGrid.OutlookGridRow row1 = (OutlookGrid.OutlookGridRow)x;
            OutlookGrid.OutlookGridRow row2 = (OutlookGrid.OutlookGridRow)y;
            string source1 = row1.Cells[0].Value.ToString();
            string source2 = row2.Cells[0].Value.ToString();

            if (row1.IsGroupRow || row2.IsGroupRow || source1 != source2)
            {
                return (this.m_Direction == ListSortDirection.Ascending) ?
                            source1.CompareTo(source2) : source2.CompareTo(source1);
            }
            else
            {
                string info1 = row1.Cells[1].Value.ToString().Trim();
                string info2 = row2.Cells[1].Value.ToString().Trim();
                string book1 = "";
                string book2 = "";
                string cvInfo1 = "";
                string cvInfo2 = "";
                if (info1.Contains(" "))
                {
                    book1 = info1.Substring(0, info1.LastIndexOf(' '));//.Split(' ')[0];
                    book2 = info2.Substring(0, info2.LastIndexOf(' '));//.Split(' ')[0];
                    cvInfo1 = info1.Substring(info1.LastIndexOf(' ') + 1);
                    cvInfo2 = info2.Substring(info2.LastIndexOf(' ') + 1);
                }
                else
                {
                    cvInfo1 = info1;
                    cvInfo2 = info2;
                }

                int b1 = 0;
                int b2 = 0;

                if (this.m_Query != null)
                {
                    int cnt = 0;
                    for (int a = 0; a < this.m_Query.RowsReturned; a++)
                    {
                        if (this.m_Query.Results.GetFieldString(0, a).Equals(book1))
                        {
                            b1 = this.m_Query.Results.GetFieldInt(1, a);
                            cnt++;
                        }
                        if (this.m_Query.Results.GetFieldString(0, a).Equals(book2))
                        {
                            b2 = this.m_Query.Results.GetFieldInt(1, a);
                            cnt++;
                        }
                        if (cnt == 2)
                            break;
                    }
                }

                if (b1 != b2)
                {
                    return (this.m_Direction == ListSortDirection.Ascending) ?
                                b1.CompareTo(b2) : b2.CompareTo(b1);
                }
                else
                {
                    //string cvInfo1 = info1.Substring(info1.LastIndexOf(' ') + 1);//.Split(' ')[1];
                    //string cvInfo2 = info2.Substring(info2.LastIndexOf(' ') + 1);//.Split(' ')[1];
                    int c1 = Convert.ToInt32(cvInfo1.Split(':')[0]);
                    int c2 = Convert.ToInt32(cvInfo2.Split(':')[0]);
                    if (c1 != c2)
                    {
                        return (this.m_Direction == ListSortDirection.Ascending) ?
                                    c1.CompareTo(c2) : c2.CompareTo(c1);
                    }
                    else
                    {
                        int v1 = Convert.ToInt32(cvInfo1.Split(':')[1]);
                        int v2 = Convert.ToInt32(cvInfo2.Split(':')[1]);
                        return (this.m_Direction == ListSortDirection.Ascending) ?
                                    v1.CompareTo(v2) : b2.CompareTo(v1);
                    }
                }
            }
		}

		#endregion
	}

	#endregion
}
